#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

pthread_mutex_t mutex;
pthread_rwlock_t rwlock;  //must #define _GNU_SOURCE
void *tfn()
{
    srand(time(NULL));
    while(1){
        pthread_mutex_lock(&mutex);
        printf("hello");
        sleep(rand() % 3);
        printf(" world\n");
        pthread_mutex_unlock(&mutex);
        sleep(rand() % 3);
    }
    return NULL;
}
int main(int argc, char const *argv[])
{
    pthread_t tid;
    srand(time(NULL));
    int ret = pthread_mutex_init(&mutex,NULL);
    if(ret != 0){
        fprintf(stderr, "mutex init failed:%s\n", strerror(ret));
    }
    pthread_create(&tid,NULL,tfn,NULL);
    while (1)
    {
        pthread_mutex_lock(&mutex);
        printf("HELLO");
        sleep(rand() % 3);
        printf(" WORLD\n");
        pthread_mutex_unlock(&mutex);
        sleep(rand() % 3);
    }
    pthread_join(tid,NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}
